﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddInvoiceItem : FormTemplet
    {
        private long _nInvoiceID = -1L;

        private FormInvoiceInfo _frmInvoiceInfo;

        public FormAddInvoiceItem()
        {
            InitializeComponent();
        }

        public FormAddInvoiceItem(long nInvoiceID, FormInvoiceInfo frmInvoiceInfo) : this()
        {
            this._nInvoiceID = nInvoiceID;
            this._frmInvoiceInfo = frmInvoiceInfo;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.txtProductID.Text = CommonMethod.Fun1(this.txtProductID.Text);
            this.txtQuantity.Text = CommonMethod.Fun1(this.txtQuantity.Text);
            if (this.txtProductID.Text.Trim() == "" || this.txtProductID.Text.Trim().Length > 32)
            {
                MessageBox.Show("你输入的出库号格式不正确\r\n格式请参照订单里的产品序号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtProductID.Focus();
            }
            else if (!Regex.IsMatch(this.txtQuantity.Text, "^[0-9]{1,}$") || Convert.ToInt32(this.txtQuantity.Text.Trim()) <= 0)
            {
                MessageBox.Show("数量 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtQuantity.Focus();
            }
            else
            {
                InvoiceItems invoiceItems = new InvoiceItems();
                bool flag = false;
                try
                {
                    flag = invoiceItems.InsertInvoiceItem(this._nInvoiceID, this.txtProductID.Text.Trim(), Convert.ToInt32(this.txtQuantity.Text.Trim()), LoginInfo.OperatorID);
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number <= 50001)
                    {
                        if (number == 547)
                        {
                            MessageBox.Show("在数据库中没有找到你所输入的出库号码！\r\n\r\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            goto IL_283;
                        }
                        if (number == 50001)
                        {
                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            goto IL_283;
                        }
                    }
                    else
                    {
                        if (number == 50200)
                        {
                            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            goto IL_283;
                        }
                        switch (number)
                        {
                            case 50300:
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                this._frmInvoiceInfo.chkLockedData.Checked = true;
                                goto IL_283;
                            case 50301:
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                goto IL_283;
                            default:
                                switch (number)
                                {
                                    case 50401:
                                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        goto IL_283;
                                    case 50402:
                                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        goto IL_283;
                                }
                                break;
                        }
                    }
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                IL_283:;
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                invoiceItems.Dispose();
                if (flag)
                {
                    MessageBox.Show("出库项 录入成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.txtProductID.Focus();
                    this.txtProductID.SelectionStart = this.txtProductID.Text.IndexOf('-') + 1;
                    this._frmInvoiceInfo.ReloadInvoiceItemsForInsert(this.txtProductID.Text.Trim());
                }
            }
        }

        private void FormAddInvoiceItem_Activated(object sender, EventArgs e)
        {
            base.WindowState = FormWindowState.Normal;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
